package com.latinia.limsp.test;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * Clase destinada a generar los LOGS.
 * @author Latinia
 * @version 1.0
 * @since 1.0
 */
public final class SDPLog {
	private SDPLog() {
	}
	private static final Logger LOG_ERR = Logger.getLogger("errLogger");
	private static final Logger LOG_MSG = Logger.getLogger("msgLogger");
	private static final Logger LOG_MSG_OUT = Logger.getLogger("msgLoggerOut");
	
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    public static void extInfo(String msgType,String adaptorName,String msgXml){    	    	    	
    	LOG_MSG.info("["+DATE_FORMAT.format(new Date())+"]"+" "+msgType+" "+getExtAdaptor(adaptorName));
    	LOG_MSG.info("Mensaje:");
    	LOG_MSG.info(msgXml);
    }
    public static void extInfo_out(String msgType,String adaptorName,String msgXml){    	    	    	
    	LOG_MSG_OUT.info("["+DATE_FORMAT.format(new Date())+"]"+" "+msgType+" "+getExtAdaptor(adaptorName));
    	LOG_MSG_OUT.info("Mensaje:");
    	LOG_MSG_OUT.info(msgXml);
    }
    public static void outErr(Exception exception){
    	LOG_ERR.error("["+DATE_FORMAT.format(new Date())+"]");    	
    	if (exception != null) {
            final StringWriter sw = new StringWriter();
            exception.printStackTrace(new PrintWriter(sw));
            LOG_ERR.error(sw);
        }
    	
    }
    private static String getExtAdaptor(String adaptor){
    	String adaptorName="";
    	if(adaptor.charAt(0)=='l')    		
    		for(int i=adaptor.lastIndexOf("/")+1;i<adaptor.length();i++)    			
        		adaptorName+=""+adaptor.charAt(i);    		    	
    	else{
    		boolean isName=false;
        	int a=adaptor.lastIndexOf("/");
        	for (int i = 0; i < a; i++) {
        		if(isName)
        			adaptorName+=""+adaptor.charAt(i);
        		else if(adaptor.charAt(i)=='w')
        			if(adaptor.charAt(i+1)=='s')
        			{
        				adaptorName+=""+adaptor.charAt(i);
        				isName=true;
        			}
    		}
    	}
    	
    	return adaptorName;
    }
}
